<?php

namespace app\common\model\banxi;

use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * Attachment模型
 * @property string url              文件物理路径
 * @property int quote            上传(引用)次数
 * @property int last_upload_time 最后上传时间
 */
class Grade extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_grade';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [

    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function getListPage(array $where, string $page = '1', int $limit = 10): \think\Paginator
    {
        return $this->where($where)
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ]);
    }

    public function getListSelectPage(array $where, string $page = '1', int $limit = 10): \think\Paginator
    {
        return $this->where($where)
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ]);
    }

    // 查询
    public function getInfo(array $where): array|Model|null
    {
        return $this->where($where)->find();
    }


    public function saveOrUpdate(array $data): bool
    {
        $id = $data['id'] ?? null;
        if ($id == null) {
            $gradeModel = new self();
            $gradeModel->save($data);
        } else {
            $gradeModel = self::where('id', $id)->find();
            if (empty($gradeModel)) {
                throw new ValidateException('班级不存在');
            }
            $gradeModel->save($data);
        }
        return true;
    }




    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}